﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetAllChildPositionID_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetAllChildPositionID_V2];
GO
CREATE PROCEDURE [dbo].[sproc_GetAllChildPositionID_V2]
    @Position_id int
/*

======================================================
功能:    得到此职位所有的子职位ID
参数:
    @Position_id int        :    职位ID
======================================================

*/
AS
BEGIN
SET NOCOUNT ON;

-- Select all recursive child positions
WITH Position_Subtree( Position_ID )
AS
(
    SELECT p.Position_ID
        FROM dbo.UDS_Position p
        WHERE p.Super_Position_ID = @Position_ID AND p.Position_ID <> p.Super_Position_ID

    UNION ALL

    SELECT p.Position_ID
    FROM dbo.UDS_Position p INNER JOIN Position_Subtree ps ON p.Super_Position_ID = ps.Position_ID
)
SELECT DISTINCT Position_ID
FROM Position_Subtree;

END
GO